//
//  IntentHandler.swift
//  TimHomeIntent
//
//  Created by nkl on 2024/10/25.
//

import Intents

class IntentHandler: INExtension, MusicWidgetIntentHandling, MusicMeduimWidgetIntentHandling {
    func provideStyleOptionsCollection(for intent: MusicMeduimWidgetIntent, with completion: @escaping (INObjectCollection<StyleId>?, (any Error)?) -> Void) {
        let dataModels = TimDataManager.shared.fetchMediumWidgets()
        let datas = dataModels.map { mData in
            if let snap = mData.snapshot {
                return StyleId(identifier: mData.uidString, display: mData.itemModel.fullName, subtitle: nil, image: INImage(imageData: snap))
            } else {
                return StyleId(identifier: mData.uidString, display: mData.itemModel.fullName, subtitle: nil, image: nil)
            }
        }
        completion(INObjectCollection(items: datas), nil)
    }

    func provideStyleOptionsCollection(for intent: MusicWidgetIntent, with completion: @escaping (INObjectCollection<StyleId>?, (any Error)?) -> Void) {
        let dataModels = TimDataManager.shared.fetchSmallWidgets()
        let datas = dataModels.map { mData in
            if let snap = mData.snapshot {
                return StyleId(identifier: mData.uidString, display: mData.itemModel.fullName, subtitle: nil, image: INImage(imageData: snap))
            } else {
                return StyleId(identifier: mData.uidString, display: mData.itemModel.fullName, subtitle: nil, image: nil)
            }
        }
        completion(INObjectCollection(items: datas), nil)
    }

    override func handler(for intent: INIntent) -> Any {
        // This is the default implementation.  If you want different objects to handle different intents,
        // you can override this and return the handler you want for that particular intent.

        return self
    }
}
